package com.zhfi.dataapply.schedule.writer.chaincode;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.chaincode.ValuationPO;
import com.zhfi.dataapply.entity.data.vo.chaincode.ValuationVO;
import com.zhfi.dataapply.mapper.chaincode.ValuationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ValuationWriter implements ItemWriter<ValuationVO> {

    @Autowired
    private ValuationMapper mapper;

    @Override
    public void write(List<? extends ValuationVO> list) throws Exception {
        for (ValuationVO vo : list) {
            try {
                ValuationPO po = new ValuationPO();
                BeanUtils.copyProperties(vo, po);
                po.setFinancingAmountAverage(vo.getFinancingAmount().getAverage());
                po.setFinancingAmountMaximum(vo.getFinancingAmount().getMaximum());
                po.setFinancingAmountMinimum(vo.getFinancingAmount().getMinimum());

                po.setIncomeFiscalYearAverage(vo.getIncomeFiscalYear().getAverage());
                po.setIncomeFiscalYearMaximum(vo.getIncomeFiscalYear().getMaximum());
                po.setIncomeFiscalYearMinimum(vo.getIncomeFiscalYear().getMinimum());

                po.setPatentCountAverage(vo.getPatentCount().getAverage());
                po.setPatentCountMaximum(vo.getPatentCount().getMaximum());
                po.setPatentCountMinimum(vo.getPatentCount().getMinimum());

                po.setPbMqrAverage(vo.getPbMqr().getAverage());
                po.setPbMqrMaximum(vo.getPbMqr().getMaximum());
                po.setPbMqrMinimum(vo.getPbMqr().getMinimum());

                po.setPeTtmAverage(vo.getPeTtm().getAverage());
                po.setPeTtmMaximum(vo.getPeTtm().getMaximum());
                po.setPeTtmMinimum(vo.getPeTtm().getMinimum());

                po.setRiskCountAverage(vo.getRiskCount().getAverage());
                po.setRiskCountMaximum(vo.getRiskCount().getMaximum());
                po.setRiskCountMinimum(vo.getRiskCount().getMinimum());

                po.setValuationAverage(vo.getValuation().getAverage());
                po.setValuationMaximum(vo.getValuation().getMaximum());
                po.setValuationMinimum(vo.getValuation().getMinimum());

                po.setProfitFiscalYearAverage(vo.getProfitFiscalYear().getAverage());
                po.setProfitFiscalYearMaximum(vo.getProfitFiscalYear().getMaximum());
                po.setProfitFiscalYearMinimum(vo.getProfitFiscalYear().getMinimum());

                UpdateWrapper<ValuationPO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("chain_code", po.getChainCode());
                updateWrapper.eq("ipo_type", po.getIpoType());
                if (mapper.update(po, updateWrapper) <= 0) {
                    mapper.insert(po);
                }
            } catch (Exception e) {
                log.error("ValuationWriter写入数据失败：" + vo, e);
            }
        }
    }
}


